﻿/**
 * GoText Palm: A program that allows to send text messages over the internet.
 *  Copyright (C) 2008 Nicola 'Gawaine' Racco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using GoTextPalm.Core;
using GoTextPalm.Core.Localization;
using System.Globalization;
using GoTextPalm.Core.Data;
using GoTextPalm.Core.Persistence;

namespace GoTextPalm.Gui
{
    public partial class SplashForm : LocalizedForm
    {
        private Logic logic;

        public SplashForm()
        {
            logic = new Logic();
            logic.LoadProcedureEndedEvent += new Logic.LoadProcedureEndedHandler(Logic_LoadProcedureEnded);
            logic.ProgressNextedEvent += new Logic.ProgressNextedHandler(Logic_ProgressNexted);
            logic.StatusTextChangedEvent += new Logic.StatusTextChangedHandler(Logic_StatusTextChanged);

            InitializeComponent();
            UpdateResources();
        }

        private void SplashForm_Load(object sender, EventArgs e)
        {
            pbar.Maximum = 4;
            new Thread(new ThreadStart(logic.LoadProcedure)).Start();
        }

        public void Logic_StatusTextChanged(String text)
        {
            if (InvokeRequired)
            {
                Logic.StatusTextChangedHandler func = Logic_StatusTextChanged;
                Invoke(func, text);
                return;
            }
            if (!animator.Started)
                animator.Started = true;
            lblStatus.Text = text;
        }

        public void Logic_ProgressNexted()
        {
            if (InvokeRequired)
            {
                Logic.ProgressNextedHandler func = Logic_ProgressNexted;
                Invoke(func);
                return;
            }
            pbar.Value++;
        }

        public void Logic_LoadProcedureEnded()
        {
            if (InvokeRequired)
            {
                Logic.LoadProcedureEndedHandler func = Logic_LoadProcedureEnded;
                Invoke(func);
                return;
            }
            animator.Started = false;
            Close();
        }

        private class Logic
        {
            public delegate void StatusTextChangedHandler(String text);
            public event StatusTextChangedHandler StatusTextChangedEvent;

            public delegate void ProgressNextedHandler();
            public event ProgressNextedHandler ProgressNextedEvent;

            public delegate void LoadProcedureEndedHandler();
            public event LoadProcedureEndedHandler LoadProcedureEndedEvent;

            public void LoadProcedure()
            {
                FireStatusTextChanged(Utility.Tr("ServiceLogic_sts5", "Checking Database ..."));
                if (!DB.ExistsDB())
                {
                    FireProgressNexted();
                    FireStatusTextChanged(Utility.Tr("ServiceLogic_sts6", "Database doesn't exists. Creation ..."));
                    DB.CreateDB();
                    FireProgressNexted();
                    FireStatusTextChanged(Utility.Tr("ServiceLogic_sts7", "Database Population in progress ..."));
                    DB.InstallDB();
                }
                else
                {
                    FireProgressNexted();
                    FireStatusTextChanged(Utility.Tr("ServiceLogic_sts10", "Checking Database Version ..."));
                    String _build_of = DBConfig.get("_build_of_");
                    FireProgressNexted();
                    if (_build_of == null || _build_of == String.Empty)
                    {
                        FireStatusTextChanged(Utility.Tr("ServiceLogic_sts11", "Updating Database"));
                        DB.UpdateDB();
                    }
                }
                FireProgressNexted();
                FireStatusTextChanged(Utility.Tr("ServiceLogic_sts8", "Connection to Database ..."));
                DB a = DB.Instance;

                FireStatusTextChanged(Utility.Tr("ServiceLogic_sts9", "Fetching Language ..."));
                String value = DBConfig.get("lang");
                if (value == null)
                {
                    CultureInfo ci = CultureInfo.CurrentUICulture;
                    Language l = Languages.GetLanguage(ci);
                    if (l != null)
                        ServiceLogic.SetLanguage(l);
                    else
                        ResourceManager.Instance.Culture = ci;
                    DBConfig.set("lang", ResourceManager.Instance.Culture.Name);
                }
                else
                {
                    Language l = Languages.GetLanguage(value);
                    if (l != null)
                        ServiceLogic.SetLanguage(l);
                }
                FireLoadProcedureEnded();
            }

            private void FireStatusTextChanged(String text)
            {
                if (StatusTextChangedEvent != null)
                    StatusTextChangedEvent(text);
            }

            private void FireProgressNexted()
            {
                if (ProgressNextedEvent != null)
                    ProgressNextedEvent();
            }

            private void FireLoadProcedureEnded()
            {
                if (LoadProcedureEndedEvent != null)
                    LoadProcedureEndedEvent();
            }
        }
    }
}